package com.opennetexam.manager.controller;

import com.opennetexam.manager.bussinessservice.ExamMonitorService;
import com.opennetexam.manager.common.CommonFunction;
import com.opennetexam.manager.common.OperationResult;
import com.opennetexam.manager.common.RoomControl;
import com.opennetexam.manager.databasemodel.DeregulationItem;
import com.opennetexam.manager.databasemodel.ExamAdminUser;
import com.opennetexam.manager.databasemodel.ExamingMonitor;
import com.opennetexam.manager.dto.AdminInfo;
import com.opennetexam.manager.dto.ExamAdminInfoDTO;
import com.opennetexam.manager.dto.ExamPlanInfoTime;
import com.opennetexam.manager.dto.ExamStudentInfoViewModel;
import com.opennetexam.manager.enums.OperationResultType;
import com.opennetexam.manager.enums.ParameterKey;
import com.opennetexam.manager.utils.DateUtil;
import com.opennetexam.manager.utils.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 奶茶
 * Created on 2017/11/15.
 */
@Controller
@RequestMapping(value = "/ExamMonitor")
public class ExamMonitorController extends BaseController{

    @Autowired
    private ExamMonitorService examMonitorService;

    /**
     * 获取正式或模拟的考试监控列表
     */
    @RequestMapping(value = "/GetList",method = RequestMethod.POST)
    public void getList(@RequestBody Map<String,Object> requestBody, HttpServletResponse response){
        String account = (String) requestBody.get("account");
        Integer planType = (Integer) requestBody.get("planType");
        ExamAdminInfoDTO adminUserInfo = getAdminUserInfo(account);
        List<ExamPlanInfoTime> list = examMonitorService.notOverExamPlanInfo(adminUserInfo.getExamSiteID(), planType.shortValue());
        Map<String,Object> map = new HashMap<>(3);
        map.put("serviceTime", DateUtil.convertEncryptDateToString(new Date()));
        map.put("list",list);
        CommonFunction.responseWrite(response,map);
    }

    /**
     * 获取考生考试情况列表
     * @param requestBody
     * @param response
     */
    @RequestMapping(value = "/GetExamcaseList",method = RequestMethod.POST)
    public void getExamcaseList(@RequestBody Map<String,Object> requestBody, HttpServletResponse response){
        String account = (String) requestBody.get("account");
        Boolean anImport = Boolean.valueOf(requestBody.get("anImport").toString());
        Object examCaseList = examMonitorService.examCaseList(getAdminUserInfo(account), requestBody);
        if(anImport){
            Map<String,Object> returnMap = new HashMap<>(2);
            if(examCaseList == null){
                returnMap.put("ResultType",OperationResultType.Error);
                returnMap.put("Message","暂无可以导出数据！");

            }else{
                returnMap.put("ResultType",OperationResultType.Success);
                returnMap.put("Message",examCaseList.toString());
            }
            CommonFunction.responseWrite(response,returnMap);
        }else{
            CommonFunction.responseWrite(response,examCaseList);
        }
    }

    /**
     * 强制交卷
     * @param requestBody
     * @param response
     */
    @RequestMapping(value = "/MandatoryPaper",method = RequestMethod.POST)
    public void mandatoryPaper(@RequestBody Map<String,Object> requestBody,HttpServletResponse response){
        //编排ID
        String arrangeId = requestBody.get(ParameterKey.ARRANGEID.getDisplay()).toString();
        //准考证ID
        String examCardNo = (String) requestBody.get("examCardNo");

        OperationResult operationResult = examMonitorService.mandatoryInfo(arrangeId);
        if(operationResult.getResultType() == OperationResultType.Success){
            ExamingMonitor monitor = (ExamingMonitor) operationResult.getAppendData();
            String key = String.format("%s_%s_%s_%s_%s_%s",monitor.getAppID(),monitor.getExamPlanInfoID(),monitor.getExamSceneID(),monitor.getExamRoomID(),monitor.getExamSubjectID(),examCardNo).toLowerCase();
            if(!RoomControl.getMandatoryPaperList().contains(key)){
                RoomControl.getMandatoryPaperList().add(key);
                operationResult.setAppendData(null);
            }
        }

        CommonFunction.responseWrite(response,operationResult);
    }


    /**
     * 标记作弊
     * @param requestBody
     * @param response
     */
    @RequestMapping(value = "/SignDeregulation",method = RequestMethod.POST)
    public void signDeregulation(@RequestBody Map<String,Object> requestBody,HttpServletResponse response){
        String arrangeId = requestBody.get(ParameterKey.ARRANGEID.getDisplay()).toString();
        Short deregulationId = Integer.valueOf(requestBody.get("deregulationId").toString()).shortValue();

        OperationResult operationResult = examMonitorService.disciplineExamInfo(arrangeId, deregulationId);
        CommonFunction.responseWrite(response,operationResult);
    }


    /**
     * 获取学生基本信息
     * @param requestBody
     * @param request
     * @param response
     */
    @RequestMapping(value = "/GetStudentInfo",method = RequestMethod.POST)
    public void getStudentInfo(@RequestBody Map<String,Object> requestBody,HttpServletRequest request,HttpServletResponse response) throws Exception {
        String examineeId = (String) requestBody.get("examineeId");
        String subjectId = (String) requestBody.get("subjectId");
        String planId = (String) requestBody.get(ParameterKey.PLANID.getDisplay());
        String account = (String) requestBody.get("account");
        ExamStudentInfoViewModel stuInfo = examMonitorService.getStuInfo(examineeId, getAdminUserInfo(account).getAppID(), planId,subjectId);
        CommonFunction.responseWrite(response,stuInfo);
    }

    /**
     * 获取违纪类型列表
     */
    @RequestMapping(value = "/GetDeregulationList",method = RequestMethod.POST)
    public void getDeregulationList(HttpServletResponse response){
        List<DeregulationItem> deregulationItems = examMonitorService.deregulationItemList();
        CommonFunction.responseWrite(response,deregulationItems);
    }

    /**
     * 判断是否进行过整场延时
     * @param response
     */
    @RequestMapping(value = "/GetIsRoomDelayed",method = RequestMethod.POST)
    public void getIsRoomDelayed(HttpServletResponse response){
        CommonFunction.responseWrite(response,false);
    }
}
